package com.globant.AutomateEMR;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class FileManager {
	
	public void deleteFiles(File reportDir){
		if(reportDir.isDirectory()){
			File files[] = reportDir.listFiles();
			for (int f = 0; f <files.length; f++) {
				files[f].delete();
			}
			reportDir.delete();
		}else{
			reportDir.delete();
			System.out.println("Report Dir:"+reportDir.getName()+" deleted");
		}
	}
	public static void createDirectories(File toDatabase){
		File akn_session_fact=new File(toDatabase,"akn_session_fact");
		 akn_session_fact.mkdir();
		File akn_query_fact=new File(toDatabase,"akn_query_fact");
		akn_query_fact.mkdir();
		File akn_NoResultQuery_fact=new File(toDatabase,"akn_noresultquery_fact");
		akn_NoResultQuery_fact.mkdir();
		File akn_traffic_fact=new File(toDatabase,"akn_traffic_fact");
		akn_traffic_fact.mkdir();
		File akn_word_fact=new File(toDatabase,"akn_word_fact");
		akn_word_fact.mkdir();
	}
	public static void createExtraDirectories(File extraInformation){
		File NewLogs=new File(extraInformation,"NewLogs");
		NewLogs.mkdir();
		File RegsToEmail=new File(extraInformation,"RegsToEmail");
		RegsToEmail.mkdir();
		File SessionFilterNotReady=new File(extraInformation,"SessionFilterNotReady");
		SessionFilterNotReady.mkdir();
		File ParsedLogs=new File(extraInformation,"ParsedLogs");
		ParsedLogs.mkdir();
	}
	public File moveFile(File file, String folderToMove) {
		File resultFile=null;
		try{		
    	   if(file.renameTo(new File(folderToMove +"/"+ file.getName()))){
    		System.out.println("File is moved successful!");
    		resultFile=new File(folderToMove +"/"+ file.getName());
    	   }else{
    		System.out.println("File is failed to move!");
    	   } 
	   }catch(Exception e){
    		e.printStackTrace();
	   }
		return resultFile;
	}
	public String generateLogId(File log){
		String logId=null;
		String log_name=log.getName();
		System.out.println("Log NAME: "+log_name);
		if(log_name.contains("beehive")){
			log_name=log_name.substring(log_name.indexOf("S.")+2);
			logId=log_name.substring(0,log_name.indexOf(".gz"));
		}else if(log_name.contains("haproxy")){
			log_name=log_name.substring(log_name.indexOf("haproxy")+8);
			logId=log_name.substring(0,log_name.indexOf(".gz"));
		}
		return logId;
	}
	public void moveFile2(String pathFrom,String pathTo){
	   	InputStream inStream = null;
		OutputStream outStream = null;
	 
	    	try{
	 
	    	    File afile =new File("pathFrom");
	    	    File bfile =new File("pathTo");
	 
	    	    inStream = new FileInputStream(afile);
	    	    outStream = new FileOutputStream(bfile);
	 
	    	    byte[] buffer = new byte[1024];
	 
	    	    int length;
	    	    //copy the file content in bytes 
	    	    while ((length = inStream.read(buffer)) > 0){
	 
	    	    	outStream.write(buffer, 0, length);
	 
	    	    }
	 
	    	    inStream.close();
	    	    outStream.close();
	 
	    	    //delete the original file
	    	    afile.delete();
	 
	    	    System.out.println("File is copied successful!");
	 
	    	}catch(IOException e){
	    	    e.printStackTrace();
	    	}
	}

	public boolean validateLog(File file) {
		boolean result=false;
		try{
			if(!file.getName().contains("`")){
				String logId=this.generateLogId(file);
				if(logId!=null){
					result=true;
				}
			}
		}catch(Exception e){
			
		}
		return result;
	}
}
